package com.wavehi.web.homework;

import com.wavehi.web.BaseServlet;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 22/9/2021 下午 9:09
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebServlet("/homework04Servlet")
public class Homework04Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //cookie的使用
        //cookie的创建
        Cookie cookie = new Cookie("username", "Tom");
        //获取cookie的名称
        String cookieName = cookie.getName();
        //设置获取获取cookie的值
        String cookieValue = cookie.getValue();
        cookie.setValue("Betty");
        //设置或获取cookie的生命周期
        cookie.setMaxAge(2);
        int maxAge = cookie.getMaxAge();
        //将cookie回传客户端
        resp.addCookie(cookie);
        //从客户端获取cookie
        Cookie[] cookies = req.getCookies();

        //session的使用
        //获取session
        HttpSession session = req.getSession();
        //获取session的id值
        String id = session.getId();
        //设置或获取session域中的数据
        session.setAttribute("username", "admin");
        Object username = session.getAttribute("username");
        //设置或获取session的请求间隔时间
        session.setMaxInactiveInterval(60);
        int maxInactiveInterval = session.getMaxInactiveInterval();
        //使session无效
        session.invalidate();
    }
}
